import 'package:cloud_medicine/model/user_address.dart';
import 'package:cloud_medicine/pages/address_new.dart';
import 'package:cloud_medicine/pages/address_edit.dart';
import 'package:flutter/material.dart';

class AddressList extends StatefulWidget {
  final List<AddressItem> addressList;
  final ValueChanged callback;
  AddressList({Key key, this.callback, this.addressList}) : super(key: key);

  @override
  _AddressListState createState() => _AddressListState();
}

class _AddressListState extends State<AddressList> {
  int _groupValue = 0;

  _radioValueChanged(int value) {
    setState(() {
      _groupValue = value;
    });
    widget.callback(value);
    Future.delayed(Duration(milliseconds: 200), _navagator);
  }

  _navagator() {
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
        children: widget.addressList.map((e) {
      return Container(
        child: Row(
          children: [
            Radio(
              groupValue: _groupValue,
              value: e.ordernumber,
              onChanged: _radioValueChanged,
            ),
            Expanded(
                child: Address(
              e: e,
            ))
          ],
        ),
      );
    }).toList());
  }
}

class AddAddress extends StatelessWidget {
  const AddAddress({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: FlatButton(
          color: Colors.blue,
          onPressed: () {
            Navigator.of(context)
                .push(MaterialPageRoute(builder: (context) => NewAddress()));
          },
          child: Text('新增收货地址', style: TextStyle(color: Colors.white))),
    );
  }
}

class Address extends StatefulWidget {
  final AddressItem e;
  Address({Key key, this.e}) : super(key: key);

  @override
  _AddressState createState() => _AddressState();
}

class _AddressState extends State<Address> {
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              margin: EdgeInsets.only(bottom: 2),
              child: Text(
                widget.e.address,
                style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
              ),
            ),
            Row(children: [
              Text(
                '${widget.e.name} ${widget.e.phone}',
                style: TextStyle(color: Colors.grey[500]),
              ),
            ])
          ],
        ),
        IconButton(
            icon: Icon(Icons.edit),
            onPressed: () {
              Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) => EditAddress(
                        id: widget.e.id,
                        address: widget.e.address,
                        phone: num.parse(widget.e.phone),
                        name: widget.e.name,
                      )));
            })
      ],
    );
  }
}
